package com.sjzd.meeting.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjzd.meeting.dao.MeetingPublishDAO;
import com.sjzd.meeting.pojo.MeetingPublish;
import com.sjzd.meeting.pojo.MeetingPublishRank;
import com.sjzd.meeting.pojo.MeetingType;
import com.sjzd.meeting.service.IMeetingPublishService;
import com.sjzd.weixin.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class MeetingPublishServiceImpl implements IMeetingPublishService {

    @Autowired
    private MeetingPublishDAO meetingPublishDAO;


    /**
     *  插入发单表时
     * @param meetingPublish
     * @return
     */
    @Transactional
    @Override
    public int save(MeetingPublish meetingPublish) {
        Date createTime = new Date();
        // 发单表数据
        String mcode = createCode(createTime);
        meetingPublish.setId(UUID.randomUUID().toString());
        meetingPublish.setCreateTime(createTime);
        meetingPublish.setMcode(mcode);
        meetingPublish.setStatus(true);
        return meetingPublishDAO.insert(meetingPublish);
    }

    @Override
    public PageInfo<MeetingPublish> selectByUid(Integer uid, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeetingPublish> meetingPublishes = meetingPublishDAO.selectPublishMeetingByUid(uid);
        PageInfo<MeetingPublish> pageInfo = new PageInfo<>(meetingPublishes, pageSize);
        return pageInfo;
    }

    @Override
    public MeetingPublish selectPublishMeetingDetailById(String id) {
        return meetingPublishDAO.selectPublishMeetingDetailById(id);
    }

    @Override
    public List<MeetingPublishRank> selectPublishMeetingRank() {
        return meetingPublishDAO.selectPublishMeetingRank();
    }


    /**
     * -- 昨天的发单总量
     * -- select count(id) from meeting_publish where datediff(now(), create_time) = 1
     * -- 今天可抢的订单量
     * -- select count(id) from meeting_publish  where id not in (select mid from meeting_grab where match_status = 1);
     * -- 各个类别的可抢单量
     * -- select mt.tname, count(mt.id) from meeting_type mt
     * -- left join meeting_publish pub on pub.tid = mt.id and pub.id not in (select mid from meeting_grab where match_status = 1)
     * -- group by mt.id
     * -- 昨天成功匹配多少单
     * select count(id) from meeting_grab where datediff(now(), create_time) = 1 and match_status = 1;
     * @return
     */
    public String everyNews() {
        Integer countYesterdayPublished = meetingPublishDAO.countYesterdayPublished();
        Integer countTodayCanGrab = meetingPublishDAO.countTodayCanGrab();
        List<MeetingType> meetingTypes =  meetingPublishDAO.countEveryTypeCanGrab();
        Integer countYesterdaySuccMatched =   meetingPublishDAO.countYesterdaySuccMatched();

        StringBuilder sb = new StringBuilder();
        sb.append("每日日报("+ DateUtils.getTodayDayAndMonth() +"):\n")
                .append("\t1.昨天的发单总量是:" + countYesterdayPublished + "\n")
                .append("\t2.昨天单总成功匹配订单总量是:" + countYesterdaySuccMatched + "\n")
                .append("\t3.今天可抢的订单量是："+ countTodayCanGrab + "\n");
        for (MeetingType mt : meetingTypes) {
            sb.append("\t\t" + mt.getTname()+":"+mt.getCount()+"\n");
        }
        return sb.toString();
    }



    /**
     *  根据会议创建时间生成编号，假设一天内总共会议小于1000
     *  例如： 201212230001
     * @param createTime
     * @return
     */
    private String createCode(Date createTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(createTime);
        String maxCode = meetingPublishDAO.selectMaxMcodeByMtime(format);
        if (StringUtils.isEmpty(maxCode)) { // 当天的第一单
            return format + "0001";
        }
        return Long.parseLong(maxCode) + 1 + "";
    }
}
